package top.csy8888.work3.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import top.csy8888.work3.dao.ProductMapper;
import top.csy8888.work3.entity.Product;
import top.csy8888.work3.service.ProductService;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author CSY
 * @date 2025 - 09 - 27
 */
@Service
public class ProductServiceImpl implements ProductService {
    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private RedisTemplate<String,Product> redisTemplate;

    @Override
    public List<Product> listProduct() {
        return List.of();
    }

    @Override
    public Product getProductById(Integer id) {
        String strId = "product:" + id;
        if (!redisTemplate.hasKey(strId)) {
            Product product = productMapper.getProductById(id);
            redisTemplate.opsForValue().set(strId,product);
            redisTemplate.expire(strId,30, TimeUnit.MINUTES);
        }
        return redisTemplate.opsForValue().get(strId);
    }



}
